import tmp from "./tmp";
import { useState } from "react";

const useLocalStorage = (key, initialValue) => {
  const [value, setValue] = useState(() => {
    const storedValue = localStorage.getItem(key);
    return storedValue ? JSON.parse(storedValue).data : initialValue;
  });

  const setStoredValue = (newValue) => {
    setValue(newValue);
    localStorage.setItem(key, JSON.stringify({ data: newValue }));
  };

  return [value, setStoredValue];
};

const useData = () => {
  // todo fetch data from server
  const [config, setConfig] = useLocalStorage("config", tmp);
  return {
    config,
    setConfig,
  };
};

export default useData;
